package com.shenghuai.bclient.stores.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.leige.mytimerpickdemo.R;
import com.shenghuai.bclient.stores.util.DensityUtils;

import java.util.ArrayList;

public class MyTimePicker extends LinearLayout implements PickerView.ISelectTextSizeListener {

    public int currentHour;

    public int currentMinute;
    private PickerView hourPickView;
    private TextView hourTv;
    private TextView minuTv;
    private boolean isShowCountDown;
    private PickerView minutePickView;

    public OnTimeChangedListener onTimeChangedListener;
    private View viewEmpty;

    public MyTimePicker(Context context) {
        super(context);
        init(context, null);
    }

    public MyTimePicker(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public MyTimePicker(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public interface OnTimeChangedListener {
        void onTimeChanged(MyTimePicker myTimePicker, int hour, int minute);
    }

    private void init(Context context, AttributeSet attributeSet) {
        LayoutInflater.from(context).inflate(R.layout.view_custom_timepicker, this, true);
        this.hourPickView = findViewById(R.id.hour_pv);
        this.minutePickView = findViewById(R.id.minute_pv);
        this.hourTv = findViewById(R.id.hourTv);
        this.minuTv = findViewById(R.id.minuteTv);
        this.viewEmpty = findViewById(R.id.viewEmpty);
        setCountDownType(this.isShowCountDown);
    }

    public void setCountDownType(boolean showCountDown) {
        this.isShowCountDown = showCountDown;
        initData(showCountDown);
        Context ctx = getContext();
        if (showCountDown) {
            this.hourTv.setText(ctx.getString(R.string.countDown_hour));
            this.minuTv.setText(ctx.getString(R.string.countDown_minute));
        } else {
            this.hourTv.setText(ctx.getString(R.string.timing_hour));
            this.minuTv.setText(ctx.getString(R.string.timing_minute));
        }
        if (showCountDown) {
            this.viewEmpty.setVisibility(View.VISIBLE);
            return;
        }
        this.viewEmpty.setVisibility(View.GONE);
        this.hourPickView.setiSelectTextSizeListener(this);
    }

    private void initData(boolean showCountDown) {

        String str;
        ArrayList<String> hourList = new ArrayList<>();
        ArrayList<String> minList = new ArrayList<>();
        int i = 0;
        if (showCountDown) {
            for (int j = 0; j < 24; j++) {
                hourList.add("" + j);
            }
            while (i < 60) {
                minList.add("" + i);
                i++;
            }
        } else {

            for (int j = 0; j < 24; j++) {
                if (j < 10) {
                    str = "0" + j;
                } else {
                    str = "" + j;
                }
                hourList.add(str);
            }
            while (i < 60) {
                if (i < 10) {
                    str = "0" + i;
                } else {
                    str = "" + i;
                }
                minList.add(str);
                i++;
            }
        }
        this.hourPickView.setData(hourList, PickerView.viewTypeHour);
        this.minutePickView.setData(minList, PickerView.viewTypeMinute);
        this.hourPickView.setOnSelectListener(new PickerView.onSelectListener() {
            public void onSelect(String str) {
                int unused = MyTimePicker.this.currentHour = Integer.parseInt(str);
                if (MyTimePicker.this.onTimeChangedListener != null) {
                    OnTimeChangedListener changedListener = MyTimePicker.this.onTimeChangedListener;
                    MyTimePicker myTimePicker = MyTimePicker.this;
                    changedListener.onTimeChanged(myTimePicker, myTimePicker.currentHour, MyTimePicker.this.currentMinute);
                }
            }
        });
        this.minutePickView.setOnSelectListener(new PickerView.onSelectListener() {
            public void onSelect(String str) {
                MyTimePicker.this.currentMinute = Integer.parseInt(str);
                if (MyTimePicker.this.onTimeChangedListener != null) {
                    OnTimeChangedListener changedListener = MyTimePicker.this.onTimeChangedListener;
                    MyTimePicker myTimePicker = MyTimePicker.this;
                    changedListener.onTimeChanged(myTimePicker, myTimePicker.currentHour, MyTimePicker.this.currentMinute);
                }
            }
        });
    }

    public void setCurretHourAndMinute(int i, int i2) {
        setCurrentHour(i);
        setCurrentMinute(i2);
        OnTimeChangedListener onTimeChangedListener2 = this.onTimeChangedListener;
        if (onTimeChangedListener2 != null) {
            onTimeChangedListener2.onTimeChanged(this, this.currentHour, this.currentMinute);
        }
    }

    private void setCurrentHour(int i) {
        this.currentHour = i;
        this.hourPickView.setSelected(i);
    }

    private void setCurrentMinute(int i) {
        this.currentMinute = i;
        this.minutePickView.setSelected(i);
    }

    public void setCurrentHour(String str) {
        this.currentHour = Integer.parseInt(str);
        this.hourPickView.setSelected(str);
    }

    public void setCurrentMinute(String str) {
        this.currentMinute = Integer.parseInt(str);
        this.minutePickView.setSelected(str);
    }

    public int getCurrentHour() {
        return this.currentHour;
    }

    public int getCurrentMinute() {
        return this.currentMinute;
    }

    public void setOnTimeChangedListener(OnTimeChangedListener onTimeChangedListener2) {
        this.onTimeChangedListener = onTimeChangedListener2;
    }

    public void onTextSize(float textSize) {
        if (this.isShowCountDown) {
            this.hourTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18.0f);
            this.minuTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18.0f);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) this.hourTv.getLayoutParams();
            layoutParams.topMargin = (int) DensityUtils.INSTANCE.dp2px(getContext(), 74.0f);
            this.hourTv.setLayoutParams(layoutParams);
            return;
        }

//        this.hourTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
//        this.minuTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);

        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) this.hourTv.getLayoutParams();
//        lp.topMargin = (int) DensityUtils.INSTANCE.dp2px(getContext(), 51.0f);
        lp.topMargin = (int) DensityUtils.INSTANCE.dp2px(getContext(), 74.0f);
        lp.leftMargin = 40;
        lp.rightMargin = 40;
        this.hourTv.setLayoutParams(lp);

        lp = (LinearLayout.LayoutParams) this.minuTv.getLayoutParams();
        lp.topMargin = (int) DensityUtils.INSTANCE.dp2px(getContext(), 74.0f);
        lp.leftMargin = 40;
        this.minuTv.setLayoutParams(lp);
    }
}